<template>
      <el-container class="home-container">
        <!-- 头部 -->
        <el-header>
          <div class="left">
            <img src="../assets/2.jpeg">
            <span>黑马后台管理系统</span>
          </div>
          <el-button typ="success" @click="logout">退出登录</el-button>
        </el-header>
        <!-- 内容 -->
        <el-container>
        <!-- 内容侧边栏 -->
        <el-aside :width="isShow ? '64px' : '200px'">
          <div class="toggleMenu" @click="isShow = !isShow">
            |||
          </div>
          <el-menu
            :default-active="defaultActive"
            class="el-menu-vertical-demo"
            background-color="#373d41"
            text-color="#fff"
            :unique-opened="true"
            :collapse="isShow"
            :collapse-transition='false'
            :router="true"
            active-text-color="#409eff">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id" >
              <!-- 一级导航标题 -->
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+item2.path" v-for="item2 in item.children" :key="item2.id">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      isShow: false,
      defaultActive: '',
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },

  created () {
    this.getMenuList()
    this.defaultActive = this.$route.path
  },

  methods: {
    logout () {
      this.$router.push('/login')
    },
    // 获取数据
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.menuList = res.data
    }
  }
}
</script>

<style scoped lang='less'>
  .home-container {
    height: 100%;
    .el-header {
      background: #373d41;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        img {
          width: 60px;
          height: 60px;
          border-radius: 50%;
        }
        span {
          font-size: 24px;
          margin-left: 10px;
          color: #fff
        }
      }
    }
  // 内容
  .el-container {
    .el-aside {
      background: #373d41;
      .toggleMenu {
        width: 100%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        color: #fff;
        letter-spacing: 5px;
        cursor: pointer;
        background-color: skyblue;
        user-select: none;
      }
      .el-menu {
        border-right: none;
      }
      .iconfont {
        margin-right: 10px;
      }
    }

    .el-main {
      background: #eaedf1;
    }
  }
}
</style>
